<?php

class news2_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function gets($groupby="''",$q=null,$config)
    {
        if($config['group']=="none")$groupby="''";
        $sort=$config['sort'];
        $query = $this->db->query("
            SELECT * ,$groupby as `group`
            FROM `news` 
            WHERE `delete` is null 
            ".($q?" AND `news`.`title` like '%$q%'":"")."
            ORDER BY  `group` $sort,`insert` DESC
        ");
        return $query->result();
    }
    function gets_trash($groupby="''",$q=null,$config)
    {
        $query = $this->db->query("
            SELECT * ,$groupby as `group`
            FROM `news` 
            WHERE `delete` is not null 
            ".($q?" AND news.`title` like '%$q%'":"")."
            ORDER BY  `group`,`insert` DESC
        ");
        return $query->result();
    }
    function gets_public($groupby="''",$q=null,$config)
    {
        if($config['group']=="none")$groupby="''";
        $sort=$config['sort'];
        $query = $this->db->query("
            SELECT * ,$groupby as `group`
            FROM `news` 
            WHERE `delete` is null AND status='public'
            ".($q?" AND news.`title` like '%$q%'":"")."
            ORDER BY  `group` $sort,`insert` DESC
        ");
        return $query->result();
    }
    function gets_private($groupby="''",$q=null,$config)
    {
        $query = $this->db->query("
            SELECT * ,$groupby as `group`
            FROM `news` 
            WHERE `delete` is null AND (status='private' OR status is null)
            ".($q?" AND news.`title` like '%$q%'":"")."
            ORDER BY  `group`,`insert` DESC
        ");
        return $query->result();
    }
    function get($id)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `news` 
            WHERE `delete` is null
                AND `id`='$id'
        ");
        return $query->result();
    }
    function delete($id){
        $query = "
            UPDATE `news` 
            SET `delete` = now()
            WHERE `id`='$id'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
        
    }
    
    function restore($id){
        $query = "
            UPDATE `news` 
            SET     `delete`    =   null
            WHERE   `id`        =   '$id'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function changeStatus($id,$status){
        $query = "
            UPDATE `news` 
            SET `status`    =   '$status',
                `update`    =   now()
            WHERE `id`='$id'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function insert($params){
        $this->db->set('insert', 'NOW()', FALSE);
        $this->db->insert('news', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($id,$params){
        $this->db->where('id', $id);
        $this->db->set('update', 'NOW()', FALSE);
        $this->db->update('news', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
}
?>
